﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Volo.Abp.Reflection;
using Xms.Abp.Core;

namespace Xms.Abp.Authorization;

public abstract class PermissionsConstBase<TConst>: IPermissionsConst
{
    protected const string Spliter = ".";

    public static Dictionary<string, string> ToDictionary()
    {
        return PermissionsConst.ToDictionary(typeof(TConst));
    }

    public static string[] GetAll()
    {
        return PermissionsConst.GetAll(typeof(TConst));
    }
}

public static class PermissionsConst
{
    private static ConcurrentDictionary<Type, Dictionary<string, string>> PermissionsCache { get; } = new ConcurrentDictionary<Type, Dictionary<string,string>>();
    private static ConcurrentDictionary<Type, string[]> AllPermissionsCache { get; } = new ConcurrentDictionary<Type, string[]>();
    public static Dictionary<string, string> ToDictionary(Type type)
    {
        return PermissionsCache.GetOrAdd(type, t => ReflectionHelpers.GetPublicConstantsRecursivelyToDictionary(type));
    }
    public static string[] GetAll(Type type)
    {
        return AllPermissionsCache.GetOrAdd(type, t => ReflectionHelper.GetPublicConstantsRecursively(type));
    }
}
